<template>
    <container type="top-header" :show="{header:formShow}" class="g-width100 g-height100" header-style="height:auto;" main-style="padding:0;">
        <Form slot="header" inline
              submit-bus-key="PermanentMaterialManageList"
              ref="form"
              :form-items="formItems"
              label-width=""
        ></Form>
        <TableCard slot="main"
               ref="table"
               form-submit-bus-key="PermanentMaterialManageList"
               :operations="tableOperations"
               unique-label="media_id" key-prop="media_id"
               v-on:toolbarSearchClick="formShow = !formShow"
        >
            <template #default="scope">
                <MpMaterialNewsPreview
                        :news-item="scope.row.content.articles"
                        :show-add-button="false"
                ></MpMaterialNewsPreview>
            </template>
        </TableCard>
    </container>
</template>

<script>
    import Container from '../../../../../../common-util/src/components/page/Container.vue'
    import Form from '../../../../../../common-util/src/components/page/Form.vue'
    import TableCard from '../../../../../../common-util/src/components/page/TableCard.vue'
    import MpMaterialNewsPreview from '../../../../components/mp/MpMaterialNewsPreview.vue'

    export default {
        components:{
            Form,
            TableCard,
            MpMaterialNewsPreview,
            Container
        },
        name: "PermanentNews",
        data(){
            return {
                formShow: true,
                formItems:[
                    {
                        field: {
                            name: 'appCode'
                        },
                        element:{
                            type: 'select',
                            select:{
                                url: this.$baseURL.wx + '/wx/mp/config/list',
                                optionProp:{
                                    value: 'appCode',
                                    label: 'appName'
                                }
                            },
                            required: true,
                            label: '公众号',
                        }
                    },
                    {
                        element:{
                            type: 'button',
                            button:[
                                {
                                    action: 'submit',
                                    requestMethod:'get',
                                    url: this.searchUrl,
                                    label: '查询'
                                }
                            ]
                        }
                    }
                ],
                tableOperations:[
                    {
                        action: this.toAddPage,
                        label:'添加'
                    },
                    {
                        action:  this.toEditPage,
                        disabledOnMissingSelect:true,
                        label: '修改'
                    },
                    {
                        action: 'delete',
                        url: this.deleteUrl,
                        disabledOnMissingSelect:true,
                        label:'删除'
                    }
                ]
            }
        },
        methods:{
            searchUrl(form){
                if(!form.appCode){
                    this.$message.error('请选择公众号')
                    return false
                }
                return this.$baseURL.wx + '/wx/mp/material/'+ form.appCode +'/news/listPage'
            },
            getAppCode(){
                return this.$refs.form.getFormData().appCode
            },
            ensureAppCode() {
                if(!this.getAppCode()){
                    this.$message.error('请选择公众号')
                    return false
                }
                return true
            },
            toAddPage(){
                if(this.ensureAppCode()){
                    this.$router.push({path: '/mpPermanentMaterialNewsAdd', query: {appCode:this.getAppCode()}})

                }
            },
            toEditPage(row){
                if(this.ensureAppCode()){
                    this.$router.push({path: '/mpPermanentMaterialNewsEdit/' + row.mediaId, query: {appCode:this.getAppCode()}})

                }
            },
            deleteUrl(row){
                if(this.ensureAppCode()){
                    return this.$baseURL.wx + '/wx/mp/material/'+ this.getAppCode() +'/delete/' + row.mediaId

                }
                return false
            }
        }
    }
</script>

<style scoped>

</style>